package utils;

import java.io.File;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Created by thormentor on 19-05-2014.
 */
public class Folder {
    private static final String PATH_SEPARATOR = "/";

    public static final Folder USER_HOME = new Folder(System.getProperty("user.home"));

    private final String name;
    private File file;

    public Folder(String name){
        this(name, new File(name));
    }

    private Folder(String name, File file) {
        this.name = name;
        this.file = file;

        if (file.exists())
            if (file.isDirectory())
                log("ok");
            else
                error("exists and is not a directory");
        else
            if (file.mkdirs())
                log("created");
            else
                error("failed");
    }

    public Folder(File file) {
        this(file.getPath(), file);
    }

    private void error(String msg) {
        log(System.err, msg);
        System.exit(-1);
    }

    private void log(String msg) {
        log(System.out, msg);
    }

    private void log(PrintStream stream, String msg) {
        stream.println("Folder '" + name + "': " + msg);
    }

    public Folder createFolder(String name) {
        return new Folder(this.name + PATH_SEPARATOR + name);
    }

    public Path createPath(String name) {
        return Paths.get(this.name + PATH_SEPARATOR + name);
    }

    public File getFile() {
        return file;
    }

    public String getName() {
        return name;
    }
}

